#include "mainwindow.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QPushButton>
#include <QTextEdit>
#include <QPixmap>
#include <QStyle>
#include <QListWidget>
#include <QTextCodec>

#include "3rd/DMSNavigation.h"

#include "ejmain.h"
#include "ejbase.h"
#include "ejres.h"
#include "ejdes.h"
#include "ejmap.h"
#include "ejdata.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //去掉窗口标题栏
    this->setWindowFlags(Qt::FramelessWindowHint);

    //设置窗口可移动范围
    m_moveHeight = 40;

    //初始化当前鼠标帧数
    m_mouseCurrID = 0;
    QString str;
    m_cursor = QCursor(QPixmap(str.sprintf(":/res/cursor/%d.png", m_mouseCurrID)), 0, 0);
    this->setCursor(m_cursor);
    //设置定时器更新鼠标动画
    startTimer(100);

    //设置界面
    DMSNavigation * navi = new DMSNavigation;
    navi->addTab(new EjMain, tr("主页"));
    navi->addTab(new EjBase, tr("设置"));
    navi->addTab(new EjRes,  tr("资源"));
    navi->addTab(new EjDes,  tr("界面"));
    navi->addTab(new EjMap,  tr("地图"));
    navi->addTab(new EjData, tr("数据"));
    setCentralWidget(navi);

    //设置最小化、最大化、关闭按钮
    m_plusButton = new QToolButton(this);
    m_minButton = new QToolButton(this);
    m_maxButton = new QToolButton(this);
    m_closeButton = new QToolButton(this);
    m_plusButton->setIcon(QPixmap(":/res/png/plus.png"));
    m_minButton->setIcon(QPixmap(":/res/png/minisize.png"));
    m_maxButton->setIcon(QPixmap(":/res/png/maxsize.png"));
    m_closeButton->setIcon(QPixmap(":/res/png/close.png"));
    //设置鼠标移至按钮上的提示信息
    m_plusButton->setToolTip(tr("设置"));
    m_minButton->setToolTip(tr("最小化"));
    m_maxButton->setToolTip(tr("最大化"));
    m_closeButton->setToolTip(tr("关闭"));
    //设置最小化、关闭按钮的样式
    m_plusButton->setStyleSheet("background-color:transparent;");
    m_minButton->setStyleSheet("background-color:transparent;");
    m_maxButton->setStyleSheet("background-color:transparent;");
    m_closeButton->setStyleSheet("background-color:transparent;");
    //设置槽函数
    connect(m_plusButton, SIGNAL(clicked(bool)), this, SLOT(plus()));
    connect(m_minButton, SIGNAL(clicked(bool)), this, SLOT(windowMin()));
    connect(m_maxButton, SIGNAL(clicked(bool)), this, SLOT(windowMax()));
    connect(m_closeButton, SIGNAL(clicked(bool)), this, SLOT(windowClose()));

}

MainWindow::~MainWindow()
{

}


//窗口移动控制
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
       m_pressPoint = event->globalPos();
        if (m_pressPoint.y() <= this->y() + m_moveHeight)
        {
            m_moveFlag = true;
        }
        //event->accept();
    }
    else if (event->button() == Qt::RightButton)
    {
        //close();
    }
    QWidget::mousePressEvent(event);
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    QPoint m_mousePoint = event->globalPos();
    if (m_moveFlag)
    {
        QPoint tmp = m_mousePoint - m_pressPoint;
        m_pressPoint = m_mousePoint;
        move(this->pos() + tmp);
    }
    //event->accept();
    QWidget::mouseMoveEvent(event);
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if (m_moveFlag)
    {
        QPoint tmp = event->globalPos() - m_pressPoint;
        move(this->pos() + tmp);
        m_moveFlag = false;
    }

    //event->accept();
    QWidget::mouseReleaseEvent(event);
}

#if 0
void MainWindow::paintEvent(QPaintEvent *event)
{

}
#endif
void MainWindow::enterEvent(QEvent *event)
{

}

void MainWindow::leaveEvent(QEvent *event)
{

}

void MainWindow::timerEvent(QTimerEvent *)
{
    if (++m_mouseCurrID > 10)
        m_mouseCurrID = 0;
    QString str;
    m_cursor = QCursor(QPixmap(str.sprintf(":/res/cursor/%d.png", m_mouseCurrID)), 0, 0);

    this->setCursor(m_cursor);
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    int wide = width();
    m_closeButton->setGeometry(wide - 22, -3, 24, 24);
    m_maxButton->setGeometry(wide - 40, -3, 24, 24);
    m_minButton->setGeometry(wide - 58, -3, 24, 24);
    m_plusButton->setGeometry(wide - 76, -3, 24, 24);

    QWidget::resizeEvent(event);
}

void MainWindow::plus()
{

}

void MainWindow::windowMin()
{
    this->showMinimized();
}

void MainWindow::windowMax()
{
    if (this->windowState() == Qt::WindowNoState)
        return this->showMaximized();

    this->showNormal();
}

void MainWindow::windowClose()
{
    this->close();
}
